//
//  TravelsAuthor _TravelsView.m
//  艾听歌
//
//  Created by dllo on 16/5/28.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "TravelsAuthor _TravelsView.h"

#import "ModuleFirst_UserTripsModel.h"
#import "ModuleFirst_UserHomePageModel.h"

static NSString *const tableViewCell = @"tabelViewCell";
@interface TravelsAuthor__TravelsView ()<UITableViewDataSource, UITableViewDelegate>


@property (nonatomic ,strong) Author_TravelsTableViewCell *cell;

@property (nonatomic, strong) ModuleFirst_UserHomePageModel *bigModel;

@property (nonatomic, strong) NSMutableArray *tripsViewArray;


@property(nonatomic,assign)NSInteger nextPage;//记录下一页
@property(nonatomic,assign)BOOL isUpLoading;//标记上拉操作还是下拉操作，yes为上拉


@end

@implementation TravelsAuthor__TravelsView

- (instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    
    if (self) {
        
        [self createTableView];
    }
    
    return self;
}

- (void)createTableView{
    
    self.travelsTableView = [[UITableView alloc]initWithFrame:self.bounds style:UITableViewStylePlain];
    self.travelsTableView.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.travelsTableView];
    
    
    //消除分割线
    self.travelsTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    //成为代理人
    self.travelsTableView.dataSource = self;
    self.travelsTableView.delegate = self;
    
    
    //tabelView注册
    
    [self.travelsTableView registerClass:[Author_TravelsTableViewCell class] forCellReuseIdentifier:tableViewCell];
    
    //初始化数组,保存所有的数据, 这个初始化不能放在获取数据的方法里
    self.tripsViewArray = [NSMutableArray array];
    self.isUpLoading = NO;
    
    // 2.集成上拉下拉功能
    [self addHeader];
    [self addFooter];
}

#pragma mark --下拉刷新
- (void)addHeader
{
    
    
    __block TravelsAuthor__TravelsView *vc = self;//因为block块里用self会报警告,所以需要将self声明成block变量
    self.travelsTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        vc.nextPage = 1;
        vc.isUpLoading = NO;
        [vc getAFNetworkingTravelsData:vc.nextPage];
//        [self.travelsTableView reloadData];
    }];
    
    
    
  #pragma mark 自动刷新(一进入程序就下拉刷新)
    [self.travelsTableView.mj_header beginRefreshing];
    
}



#pragma mark --上拉加载更多
- (void)addFooter
{
    //    __unsafe_unretained typeof(self) vc = self;
    __block TravelsAuthor__TravelsView *vc = self;
    self.travelsTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        vc.nextPage ++;
        vc.isUpLoading = YES;//标记为上拉操作
        [vc getAFNetworkingTravelsData:vc.nextPage];
        
    }];
    
        [vc getAFNetworkingTravelsData:vc.nextPage];//请求数据

    [self.travelsTableView reloadData];
}




//(游记)网络请求数据
- (void)getAFNetworkingTravelsData:(NSInteger)nextCursor{
    
    self.bigModel = [[ModuleFirst_UserHomePageModel alloc]init];

    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    //创建一个管理者
    AFHTTPSessionManager* manager = [AFHTTPSessionManager manager];
    [manager.securityPolicy setAllowInvalidCertificates:YES];
    [manager.securityPolicy setValidatesDomainName:NO];
    //识别各种类型的网址
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"text/html", @"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css", @"text/plain", @"application/x-javascript", @"application/javascript",nil]];
    
    NSString *idd = self.UIModel.idd.stringValue;
//    NSLog(@"%ld", nextCursor);
    NSString *string = [NSString stringWithFormat:@"https://chanyouji.com/api/users/%@.json?page=%ld", idd, nextCursor];
    [manager GET:string parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        [netWorkManager stopMonitoring];
        
        [self.bigModel setValuesForKeysWithDictionary:responseObject];
        
        if (self.isUpLoading == NO) {
            //说明是下拉，就要清空数组中的数据
            [self.tripsViewArray removeAllObjects];
        }
        //给游记页面TA_TravelsView传值
        for (ModuleFirst_UserTripsModel *smallModel in self.bigModel.trips) {
            
            [self.tripsViewArray addObject:smallModel];
        }
        
        [self.travelsTableView.mj_footer endRefreshing];
        [self.travelsTableView.mj_header endRefreshing];
        
        [self.travelsTableView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
//        NSLog(@"error====%@",error);

        
    }];


}



//****必须做的******
//显示cell的方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    self.cell = [tableView dequeueReusableCellWithIdentifier:tableViewCell];
    
//    self.cell.textLabel.text = @"111";
    self.cell.cellUTModel = [self.tripsViewArray objectAtIndex:indexPath.row];
    
    return self.cell;
}

//每个分区中cell 的个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.tripsViewArray.count;
    
}

//******选做的******
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    return self.frame.size.height / 2;
    
}



@end
